package com.jnu.bookkeep;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class EditBillActivity extends AppCompatActivity {

    private int billType = 2;
    private int typeSignal = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_bill);

        EditText editText_price = findViewById(R.id.price_edit);

        RadioGroup expand_income_group = findViewById(R.id.expand_income_group);
        RadioButton income_button = findViewById(R.id.income_button);
        RadioButton expenditure_button = findViewById(R.id.expenditure_button);

        EditText editText_mark = findViewById(R.id.mark_edit);

        RadioGroup choose_type_group = findViewById(R.id.choose_type);
        RadioButton eat_button = findViewById(R.id.eat_type);
        RadioButton daily_use_button = findViewById(R.id.daily_use_type);
        RadioButton travel_button = findViewById(R.id.travel_type);
        RadioButton accommodation_button = findViewById(R.id.accommodation_type);
        RadioButton other_button = findViewById(R.id.other_type);

        EditText editText_year = findViewById(R.id.year_edit);
        EditText editText_month = findViewById(R.id.month_edit);
        EditText editText_day = findViewById(R.id.day_edit);

        Button button_OK = findViewById(R.id.button_ok);
        Button button_cancel = findViewById(R.id.button_cancel);

        Intent intent = getIntent();
        if(intent.getIntExtra("typeSignal",0)!=0){
            float price = intent.getFloatExtra("price",0);
            String mark = intent.getStringExtra("mark");
            int id = intent.getIntExtra("typeSignal",0);
            int billType = intent.getIntExtra("billType",0);
            int year = intent.getIntExtra("year",0);
            int month = intent.getIntExtra("month",0);
            int day = intent.getIntExtra("day",0);

            editText_mark.setText(mark);
            editText_price.setText(String.valueOf(price));
            editText_year.setText(String.valueOf(year));
            editText_month.setText(String.valueOf(month));
            editText_day.setText(String.valueOf(day));

            if(billType==1)
                income_button.setChecked(true);
            else if(billType==0)
                expenditure_button.setChecked(true);

            switch (id) {
                case R.drawable.eat:
                    eat_button.setChecked(true);
                    break;
                case R.drawable.daily_use:
                    daily_use_button.setChecked(true);
                    break;
                case R.drawable.travel:
                    travel_button.setChecked(true);
                    break;
                case R.drawable.accommodation:
                    accommodation_button.setChecked(true);
                    break;
                case R.drawable.others:
                    other_button.setChecked(true);
                    break;
            }

        }
        //这里还需要再写一个判断用户输入的if，一会来写

        expand_income_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if(income_button.isChecked()){
                    billType = 1;
                }
                else if(expenditure_button.isChecked()){
                    billType = 0;
                }
            }
        });

        choose_type_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if(eat_button.isChecked()){
                    typeSignal = R.drawable.eat;
                }
                else if(daily_use_button.isChecked()){
                    typeSignal = R.drawable.daily_use;
                }
                else if(travel_button.isChecked()){
                    typeSignal = R.drawable.travel;
                }
                else if(accommodation_button.isChecked()){
                    typeSignal = R.drawable.accommodation;
                }
                else if(other_button.isChecked()){
                    typeSignal = R.drawable.others;
                }
            }
        });

        button_OK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                /*
                if(billType == 2){
                    Toast.makeText(EditBillActivity.this, "请选择收入支出", Toast.LENGTH_LONG).show();
                    return;
                }
                //这里也要改 不能这么判断 要用checked
                if(typeSignal == 0){
                    Toast.makeText(EditBillActivity.this, "请选择账单类型", Toast.LENGTH_LONG).show();
                    return;
                }

                 */
//这里还需要继续判断
                intent.putExtra("price",editText_price.getText().toString());          //流水
                intent.putExtra("mark",editText_mark.getText().toString());            //备注
                intent.putExtra("date",editText_year.getText().toString() + "-" + editText_month.getText().toString() + "-" + editText_day.getText().toString());
                intent.putExtra("billType",billType);
                intent.putExtra("typeSignal",typeSignal);

                setResult(111,intent);
                EditBillActivity.this.finish();
            }
        });

        button_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setResult(110,intent);
                EditBillActivity.this.finish();
            }
        });

    }
}

